package cn.haut.online_read.utils;

import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class ComputeDigest {
    public static String digest(String pass){
        try {
            MessageDigest messageDigest=MessageDigest.getInstance("SHA-256");
            byte[] res=messageDigest.digest(pass.getBytes());
            StringBuilder passAfterDigest=new StringBuilder();
            for(byte b:res){
                int i=b&0xFF;
                if(i<16) {
                    passAfterDigest.append("0");
                }
                passAfterDigest.append(String.format("%H",i));
            }
            return passAfterDigest.toString();
        } catch (NoSuchAlgorithmException e) {
            log.error("加密密码出错");
            throw new RuntimeException(e);
        }
    }
    public static boolean sameAfterDigest(String pass,String passAfterDigest){
        return digest(pass).equals(passAfterDigest);
    }
}